package com.lingo.a2b.controller;


import com.lingo.a2b.model.entity.ConnectProjectDO;
import com.lingo.a2b.model.vo.ConnectFieldRelativeSaveVo;
import com.lingo.a2b.model.vo.FieldInfoVo;
import com.lingo.a2b.response.MyResponse;
import com.lingo.a2b.service.ConnectFieldRelativeService;
import com.lingo.a2b.service.ConnectProjectService;
import com.lingo.a2b.service.TranslateService;
import com.lingo.a2b.service.ValueConvertService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

/**
 * @author lingo
 * @date 2020/5/21 16:12
 * @Company: www.xyb2b.com
 */
@RestController
@RequestMapping("/translate")
@Api(tags = "对接转换接口")
public class TranslateController {

    @Autowired
    private TranslateService translateService;
    @Autowired
    private ValueConvertService valueConvertService;
    @Resource
    private ConnectProjectService connectProjectService;
    @Resource
    private ConnectFieldRelativeService connectFieldRelativeService;

    @PostMapping("/isExistClass")
    @ApiOperation("查找类是否存在")
    public MyResponse<?> isExist(@RequestParam String name){
        return MyResponse.success(translateService.isClassExist(name));
    }

    @PostMapping("handler/list")
    @ApiOperation("获取注册器列表")
    public MyResponse<?> listHandler(@RequestParam Integer fprojectId){
        return MyResponse.success(valueConvertService.listByProjectId(fprojectId));
    }

    @PostMapping("project/register")
    @ApiOperation("注册对接方案")
    public MyResponse<?> registerProject(@RequestBody @Valid ConnectProjectDO connectProjectDO){
        return MyResponse.success(connectProjectService.register(connectProjectDO));
    }

    @PostMapping("/handler/register")
    @ApiOperation("注册转换器")
    public MyResponse<?> registerHandler(@RequestParam String name,
                                         @RequestParam(required = false) @ApiParam(value = "项目id，如果可用范围选择了全部，则不传") Integer fprojectId,
                                         @RequestBody byte[] bytes){
        return MyResponse.success(valueConvertService.registerHandler(name,bytes,fprojectId));
    }

    @PostMapping("fieldRelative/saveOrUpdate")
    @ApiOperation("添加/更新字段关联关系")
    public MyResponse<?> saveOrUpdateFieldRelative(@RequestBody @Valid ConnectFieldRelativeSaveVo saveVo){
        return MyResponse.success(connectFieldRelativeService.saveOrUpdate(saveVo));
    }

    @PostMapping("fieldRelative/list")
    @ApiOperation("获取字段关联关系列表")
    public MyResponse<List<FieldInfoVo>> saveOrUpdateFieldRelative(@RequestParam Integer fprojectId){
        return MyResponse.success(connectProjectService.listFieldVoById(fprojectId));
    }
}
